package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.ClothingRequestDTO;
import com.example.demo.mapper.BadgeManageMapper;
import com.example.demo.mapper.ClothingManageMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.ClothingManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;


@Service
public class ClothingManageServiceImpl extends ServiceImpl<ClothingManageMapper, Clothing> implements ClothingManageService {
    @Autowired
    private ClothingManageMapper mapper;
    @Override
    public PageVo<Clothing> getClothingList(PageBo pageBo) {
        List<Clothing> logs = mapper.selectList(new QueryWrapper<>());
        return new PageVo<>(pageBo, logs);
    }

   @Override
   public List<Clothing> searchClothingList(ClothingRequestDTO request) {
       QueryWrapper<Clothing> queryWrapper = new QueryWrapper<>();
       if (request.getClothingName() != null) {
           queryWrapper.like("clothing_name", request.getClothingName());
       }
       if (request.getType() != 0) {
           queryWrapper.eq("type", request.getType());
       }
       if (request.getGender() != 0) {
           queryWrapper.eq("gender", request.getGender());
       }
       List<Clothing> result = mapper.selectList(queryWrapper);

       return result;
   }

    @Override
    public Result<String> addClothing(Clothing request) {
        Clothing newClothing = new Clothing();
        newClothing.setClothingName(request.getClothingName());
        newClothing.setType(request.getType());
        newClothing.setGender(request.getGender());
        newClothing.setImageUrl(request.getImageUrl());

        if(newClothing!=null){
            if (mapper.insert(newClothing) > 0) {
                return Result.ok("添加成功");
            } else {
                return Result.fail("添加失败");
            }
        }
        return Result.fail("添加失败,填写内容为空");
    }


    @Override
    public Result<String> updateClothingList(Clothing request) {
        // 不需要创建新的Clothing对象，可以直接用传入的request对象进行更新
        if (request != null && request.getClothingId() != 0) {
            // 使用MyBatis Plus提供的update方法
            int result = mapper.updateById(request);
            if (result > 0) {
                return Result.ok("修改成功");
            } else {
                return Result.fail("修改失败");
            }
        } else {
            return Result.fail("参数不正确或未提供主键信息");
        }
    }


    @Override
    public Clothing getById(int clothingId) {
        return mapper.selectById(clothingId);
    }

    @Override
    public void placeBadgePic(Clothing currentClothing) {
        if (currentClothing != null && currentClothing.getClothingId() != 0) {
            // 使用MyBatis Plus提供的update方法
            int result = mapper.updateById(currentClothing);}
    }

    @Override
    public Result<String> deleteClothing(Clothing clothing) {
        if(clothing!=null){
            if(mapper.deleteById(clothing)>0 && mapper.deleteCommodity(clothing)>0)
                return Result.ok("删除成功");
            else
                return Result.fail("删除失败");
        }
        return Result.fail("删除失败,选择内容为空");
    }

    @Override
    public void batchDeleteClothing(List<Clothing> clothingList) {
        mapper.batchDeleteClothing(clothingList);
        mapper.batchDeleteCommodityClothing(clothingList);
    }


}
